package com.cloud.pikachu.storage.controller;

import com.cloud.pikachu.storage.api.dto.UploadImageDTO;
import com.cloud.pikachu.storage.api.vo.ExecuteResult;
import com.cloud.pikachu.storage.api.vo.UploadImageVO;
import com.cloud.pikachu.storage.service.ImageUploadService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 文件上传处理Demo版本
 *
 * @author : ABug
 * @Date : 2019/4/29 10:14
 * @UpdateDate : 2019/4/29 10:14
 * @Version V1.0.0-demo
 **/
@RestController
@Slf4j
@RequestMapping("/storage")
public class ImageUploadController extends BaseController {
    @Autowired
    private ImageUploadService imageUploadService;

    /**
     * 测试文件上传性能使用 , 根据上传的并发量测试图片上传的性能，再决定是否进行封装定版 普通文件上传
     * @param file 上传文件
     * @param saveType 保存方式
     * @param saveDuration 保存时长
     * @param sliceList 切图方式
     * @param zipSize 压缩大小
     * @return
     * @throws IOException
     */
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "10000")
    },fallbackMethod = "uploadImageError")//10秒钟未完成 触发降级处理
    @RequestMapping(value = "/uploadImage" , method = RequestMethod.POST)
    public ExecuteResult<UploadImageVO> uploadVersion(@RequestParam(value = "file") MultipartFile file,
                                                      @RequestParam(value = "saveType" , required = false , defaultValue= "4") int saveType,
                                                      @RequestParam(value = "saveDuration" , required = false , defaultValue= "0") long saveDuration,
                                                      @RequestParam(value = "sliceList" , required = false) List<String> sliceList,
                                                      @RequestParam(value = "zipType" , required = false , defaultValue = "0") int zipSize,
                                                      @RequestHeader(value = "code" , required = false , defaultValue = "") String code){
        //1.将数据转化为DTO
        UploadImageDTO uploadImageDTO = new UploadImageDTO();
        uploadImageDTO.setSaveType(saveType);
        uploadImageDTO.setSaveDuration(saveDuration);
        uploadImageDTO.setSliceList(sliceList);
        uploadImageDTO.setZipSize(zipSize);
        uploadImageDTO.setHreadCode(code);
        uploadImageDTO.setMultipartFile(file);

        return imageUploadService.imageUpload(uploadImageDTO);
    }

    public ExecuteResult<UploadImageVO> uploadImageError(MultipartFile file, int saveType, long saveDuration,
                                   List<String> sliceList, int zipSize , String code) {
        ExecuteResult executeResult = new ExecuteResult();
        executeResult.setError(998 , "服务器资源占用较高，请稍后重试...");

        return executeResult;
    }


}
